<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/31
 * Time: 16:45
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\validate\CardParameter;
use app\api\service\Token;
use app\api\service\Card as CardService;
use app\api\validate\UserFatherValidate;
use app\lib\exception\CardException;
use app\lib\exception\WechatException;
use think\Hook;

class Card extends BaseController
{
    protected $beforeActionList=[
        'checkPrimaryScope' => ['only'=>'getQRCodeByWuId'],
    ];
    /**
     * 获取用户名片列表
     * @throws CardException
     */
    public function getCardList(){
        $uid = Token::getCurrentUid();
        $card = new CardService();
        $list = $card->getCardList($uid);
        if (empty($list)){
            throw new CardException([
                'code'=>201
            ]);
        }else{
            return json_encode($list);
        }
    }

    /**
     * 创建父级
     * @param $father
     * @param $poster 默认为扫分享码进入
     * @param $sharer 默认为扫码进入(分享名片时sharer不为0)
     * @return int
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function createFather($father,$poster = 0,$sharer = 0){
        (new UserFatherValidate())->goCheck();
        $uid = Token::getCurrentUid();
        $manager_id = Token::getCurrentTokenVar('manager_id');
        //更新用户父级
        $data = explode('_',$father);
        if ($data[0] == 'share'){
            $wu_id = $data[1];
            $source = 2;
        }elseif ($data[0]=='qrcode'){
            $wu_id = $data[1];
            $source = 1;
        }elseif($data[0]=='sowcode'){
            $wu_id = $data[1];
            $source = 3;
        }
        $card = new CardService();
        $status = $card->createFather($uid,$wu_id,$poster,$source,$sharer,$manager_id);
       return $status;
    }

    /**
     * 获取名片基本信息
     * @param $wu_id
     * @return false|string
     * @throws \think\Exception
     */
    public function getCard($wu_id){
        $uid = Token::getCurrentUid();
       //id处理
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $card = new CardService();
        $cardInfo = $card->getCardInfo($uid,$wu_id);
        return  json_encode($cardInfo);
    }
    /**
     * 用浏览记录
     * @param $wu_id
     * @return string
     * @throws CardException
     */
    public function getBrowseList($wu_id){
        $uid = Token::getCurrentUid();
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $card = new CardService();
        $list = $card->getBrowse($wu_id);
        if (empty($list)){
            throw new CardException([
                'code'=>200,
                'msg'=>'请求浏览用户不存在',
                'error_code'=>51000
            ]);
        }else{
            return json_encode($list);
        }
    }

    /**
     * 获取点赞量
     * @param $wu_id
     * @return string
     */
    public function getThumsUpNum($wu_id){
        $uid = Token::getCurrentUid();
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $card = new CardService();
        $result = $card->getThumsUpNum($uid,$wu_id);
        return json_encode($result);
    }

    /**
     * 获取标签
     * @param $wu_id
     * @return string
     */
    public function getTagNum($wu_id){
        $uid = Token::getCurrentUid();
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $card = new CardService();
        $result = $card->getTagList($wu_id);
        return json_encode($result);
    }

    /**
     * 推荐商品
     * @param $wu_id
     * @return string
     */
    public function getCardProduct($wu_id){
        $uid = Token::getCurrentUid();
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $card = new CardService();
        $result = $card->getCardProduct($wu_id);
        return json_encode($result);
    }

    /**
     * 获取名片拓展信息
     * @param $wu_id
     * @return $this
     * @throws CardException
     */
    public function getCardExtend($wu_id){
        $uid = Token::getCurrentUid();
        //id处理
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $card = new CardService();
        $cardInfo = $card->getCardExtendInfo($wu_id);
        return $cardInfo;
    }

    /**
     * 获取名片分享语
     * @param $wu_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getShareWord($wu_id){
        $uid = Token::getCurrentUid();
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $card = new CardService();
        $cardInfo = $card->getShareWord($wu_id);
        return $cardInfo;
    }

    /**
     *用户授权，获取用户详情,更新用户表
     * @return array
     * @throws WechatException
     */
    public function updateUserInfo(){
        $dataArray = input('post.');
        if ($dataArray['authorization']['detail']['errMsg'] === 'getUserInfo:ok'){//授权成功。微信返回，返回的真尼玛垃圾
            //error_log(print_r($dataArray, 1), 3, "./yzh.txt");
            $uid = Token::getCurrentUid();
            $data = $dataArray['authorization']['detail'];
            $card = new CardService();
            $status = $card->updateUser($uid,$data);
            if (!$status){
                throw new WechatException([
                    'msg'=>'用户授权失败'
                ]);
            }else{
                $info['data'] = $status;
                $info['error_code'] = 0;
                $info['msg'] = '用户授权成功';
                return json($info);
            }
        }else{//授权失败
            throw new WechatException([
                'code'=>200,
                'msg'=>'用户拒绝授权',
                'error_code'=>50004
            ]);
        }
    }

    /**
     * 用户授权
     * @return \think\response\Json
     * @throws WechatException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function userAuthorization(){
        $dataArray = input('post.');
        if ($dataArray['authorization']['errMsg'] === 'getUserInfo:ok'){//授权成功。微信返回，返回的真尼玛垃圾
            $uid = Token::getCurrentUid();
            $t_id = Token::getCurrentTokenVar('manager_id');
            $data = $dataArray['authorization']['userInfo'];
            $card = new CardService();
            $status = $card->updateUserInfo($uid,$t_id,$data);
            if (!$status){
                throw new WechatException([
                    'msg'=>'用户授权失败'
                ]);
            }else{
                $info['data'] = $status;
                $info['error_code'] = 0;
                $info['msg'] = '用户授权成功';
                return json($info);
            }
        }else{//授权失败
            throw new WechatException([
                'code'=>200,
                'msg'=>'用户拒绝授权',
                'error_code'=>50004
            ]);
        }
    }

    /**
     * 标签点赞
     * @param $w_id
     * @return int|true
     */
    public function tagClick($id){
        $uid = Token::getCurrentUid();
        $card = new CardService();
        $num = $card->clickTag($uid,$id);
        if ($num){
            return $num;
        }
    }
    /**
     * 获取手机号
     * @return array|false|int|mixed|\PDOStatement|string|\think\Model
     */
    public function getPhone($w_id){
        $uid = Token::getCurrentUid();
        $data = input('post.');
        $card = new CardService();
        $info =  $card->getAuthorizedTelephone($w_id,$uid,$data);
        if (!empty($info)){
            return $info;
        }
    }

    /**
     * 获取名片码
     * @param $w_id
     * @return \think\response\Json
     * @throws CardException
     * @throws \app\lib\exception\MiniSuiteException
     * @throws \think\Exception
     */
    public function getQRCodeByWuId($w_id){
        (new CardParameter())->goCheck();
        Token::getCurrentUid();//用户身份判断
        $card = new CardService();
        $url = $card->getQRCode($w_id);
        if (null != $url){
          $data['error_code'] = 0;
          $data['url'] = config('setting.remote_img_url').$url;
          return json($data);
        }else{
         throw new CardException([
             'code'=>200,
             'mag'=>'获取名片码失败',
             'error_code'=>50003
         ]);
        }
    }


    
}